package com.xuan.yubi.controller;


import com.xuan.yubi.common.BaseResponse;
import com.xuan.yubi.common.ErrorCode;
import com.xuan.yubi.common.ResultUtils;
import com.xuan.yubi.exception.BusinessException;
import com.xuan.yubi.exception.ThrowUtils;
import com.xuan.yubi.model.entity.User;
import com.xuan.yubi.service.UserService;
import com.xuan.yubi.utils.FileUtils;
import io.netty.util.internal.ThrowableUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/oss")
public class OssController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileUtils fileUtils;

    @PostMapping("/upload")
    public BaseResponse<String> upload(MultipartFile file, HttpServletRequest request) {
        ThrowUtils.throwIf(file.isEmpty(), ErrorCode.PARAMS_ERROR,"文件为空");
        User loginUser = userService.getLoginUser(request);
        System.out.println(loginUser);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        return ResultUtils.success(fileUtils.uploadImage(file));
    }
}
